import pygame
import sys
from board import Board
from pieces import Pieces
import random

class Game():
    def __init__(self,is_first):
        pygame.init()
        pygame.display.set_caption('wuziqi')
        self.screen = pygame.display.set_mode((600,600))
        self.board = Board()
        self.pieces = []
        self.id = 1
        self.isRun = 1
        self.is_first=is_first

    #以此命名的属性或者方法为类的私有属性或者私有方法
    def __finish(self,winner):
        if winner == self.is_first:
            print('You win!')
        else:
            print('AI win!')
        self.isRun = 0

    def __update(self):
        self.screen.fill((230,230,230))
        self.board.update(self.screen)
        [i.update(self.screen) for i in self.pieces]
        pygame.display.update()

    def __check_mouseMove(self,event):
        pass

    def __check_mouseClick(self,event):
        try:
            pos = (event.pos[0]//40,event.pos[1]//40)
            if self.board.getSite(pos) != 0: #判断是否空位
                return
            pieces = Pieces(pos[0],pos[1],self.id)
            self.pieces.append(pieces) #添加新棋子到pieces中
            self.board.move(pieces) #棋盘上更新棋子状态
            if self.board.judge(pieces)[0]: #判断下当前棋子是否可以获胜
                self.__finish(self.board.judge(pieces)[1]) #打印获胜者并isRun置0
            self.id = 1 if (self.id == 2) else 2
            self.aiPlay()
        except Exception as e:
            print(e)
        # site = random.choice(self.board.movableSite)
        # pieces = Pieces(site[0],site[1],2)
        # self.pieces.append(pieces)
        # self.board.move(pieces)
        # if self.board.judge(pieces)[0]:
        #     self.__finish(self.board.judge(pieces)[1])

    def __check(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == pygame.MOUSEMOTION:
                self.__check_mouseMove(event)
            if event.type == pygame.MOUSEBUTTONDOWN:
                self.__check_mouseClick(event)

    def aiPlay(self):
        #存储拥有最大评分的点
        X=[]
        Y=[]
        maxn=-0x7fffffff
        #遍历棋盘
        for x in range(0,15):
            for y in range(0,15):
                if self.board.place[x,y] == 0: #若当前位置为空
                    score=self.board.getScore(x,y,self.is_first) #获取当前位置的最佳评分
                    if maxn <= score:
                        if maxn < score:
                            X.clear()
                            Y.clear()
                        maxn=score
                        X.append(x)
                        Y.append(y)
        randomIdx=random.randint(0,len(X)-1)
        pieces = Pieces(X[randomIdx], Y[randomIdx], 2 if self.is_first==1 else 1)
        self.pieces.append(pieces) #下当前位置
        self.board.move(pieces)
        if self.board.judge(pieces)[0]:
            self.__finish(self.board.judge(pieces)[1])
        self.id = 1 if (self.id == 2) else 2

    def run(self,is_first):
        if self.is_first == 0:
            self.aiPlay()
        while True:
            self.__update()
            self.__check()
            pygame.time.wait(100)
            if self.isRun == 0: #若为0则表示一轮结束，需初始化重开
                self.__init__(is_first)
                if self.is_first==0:
                    self.aiPlay()